#include "binarytranslation.h"


void binary_translation(void) {
   char opcode[] = {0xb8, 0xeb, 0x8e, 0x3e, 0xcf};
   int guest_address = 0xc0050000;
   int cache_address = 0xc0051000;
   char byte_bin;
   char char_bin;
   char prefix_code;
   char guest_gs = 0xe2;
   while(1) {
      asm("movb (%%ebx), %%al":"=a"(byte_bin):"b"(guest_address));
      
      guest_address = guest_address + 0x1;
      if (byte_bin == opcode[0]) {
         prefix_code = 0x66;
         asm("movb %%al, (%%ebx)": :"a"(prefix_code), "b"(cache_address));
         cache_address = cache_address + 0x1;

         asm("movb %%al, (%%ebx)": :"a"(byte_bin), "b"(cache_address));
         cache_address = cache_address + 0x1;

         asm("movb (%%ebx), %%al":"=a"(byte_bin):"b"(guest_address));
         guest_address = guest_address + 0x1;
         asm("movb %%al, (%%ebx)": :"a"(byte_bin), "b"(cache_address));
         cache_address = cache_address + 0x1;
         asm("movb (%%ebx), %%al":"=a"(byte_bin):"b"(guest_address));
         guest_address = guest_address + 0x1;
         asm("movb %%al, (%%ebx)": :"a"(byte_bin), "b"(cache_address));
         cache_address = cache_address + 0x1;

      } else if (byte_bin == opcode[1]) {
         asm("movb %%al, (%%ebx)": :"a"(byte_bin), "b"(cache_address));
         cache_address = cache_address + 0x1;
         asm("movb (%%ebx), %%al":"=a"(byte_bin):"b"(guest_address));
         guest_address = guest_address + 0x1;
         asm("movb %%al, (%%ebx)": :"a"(byte_bin), "b"(cache_address));
         cache_address = cache_address + 0x1;
         break;
      } else if (byte_bin == opcode[2]) {
        guest_address = guest_address + 0x1;
      } else if (byte_bin == opcode[3]) {
         
         guest_address = guest_address + 0x4;
         
         asm("movb (%%ebx), %%al":"=a"(char_bin):"b"(guest_address));
         guest_address = guest_address + 0x1;
         
         byte_bin = 0x65;
         asm("movb %%al, (%%ebx)": :"a"(byte_bin), "b"(cache_address));
         cache_address = cache_address + 0x1;
         byte_bin = 0xc6;
         asm("movb %%al, (%%ebx)": :"a"(byte_bin), "b"(cache_address));
         cache_address = cache_address + 0x1;
         byte_bin = 0x05;
         asm("movb %%al, (%%ebx)": :"a"(byte_bin), "b"(cache_address));
         cache_address = cache_address + 0x1;

         byte_bin = guest_gs;
         asm("movb %%al, (%%ebx)": :"a"(byte_bin), "b"(cache_address));
         cache_address = cache_address + 0x1;
         byte_bin = 0x01;
         asm("movb %%al, (%%ebx)": :"a"(byte_bin), "b"(cache_address));
         cache_address = cache_address + 0x1;
         byte_bin = 0x00;
         asm("movb %%al, (%%ebx)": :"a"(byte_bin), "b"(cache_address));
         cache_address = cache_address + 0x1;
         byte_bin = 0x00;
         asm("movb %%al, (%%ebx)": :"a"(byte_bin), "b"(cache_address));
         cache_address = cache_address + 0x1;
         guest_gs = guest_gs + 0x2;
         
         asm("movb %%al, (%%ebx)": :"a"(char_bin), "b"(cache_address));
         cache_address = cache_address + 0x1;
         
      
      } else if (byte_bin == opcode[4]) {
         asm("movb %%al, (%%ebx)": :"a"(byte_bin), "b"(cache_address));
         cache_address = cache_address + 0x1;
      }
   }
  
}

